package aoj.no_0200;

import java.util.PriorityQueue;
import java.util.Scanner;

public class No_0200 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true) {
			int nRail = sc.nextInt();
			int nStation = sc.nextInt();
			if(nRail == 0 && nStation == 0)
				break;
			
			int[][] cost = new int[nStation+1][nStation+1];
			int[][] time = new int[nStation+1][nStation+1];
			for(int i = 0; i < nRail; i++) {
				int a = sc.nextInt();
				int b = sc.nextInt();
				cost[a][b] = cost[b][a] = sc.nextInt();
				time[a][b] = time[b][a] = sc.nextInt();
			}
			int nDataset = sc.nextInt();
			for(int i = 0; i < nDataset; i++) {
				int start = sc.nextInt();
				int goal = sc.nextInt();
				if(sc.nextInt() == 0) {
					System.out.println(solveCost(nRail, nStation, start, goal, cost));
				}
				else {
					System.out.println(solveTime(nRail, nStation, start, goal, time));
				}
			}
		}
	}
	
	private static int solveCost(int nRail, int nStation, int start, int goal, int cost[][]) {
		PriorityQueue<CostState> que = new PriorityQueue<CostState>();
		boolean[] visited = new boolean[nStation + 1];
		
		que.add(new CostState(start, 0));
		
		while(!que.isEmpty()) {
			CostState current = que.poll();
			if(visited[current.currentTown])
				continue;
			visited[current.currentTown] = true;
			
			if(current.currentTown == goal)
				return current.cost;
			
			for(int i = 1; i <= nStation; i++) {
				if(cost[current.currentTown][i] > 0) {
					que.add(new CostState(i, current.cost + cost[current.currentTown][i]));
				}
			}
		}
		return -1;
	}
	
	private static int solveTime(int nRail, int nStation, int start, int goal, int time[][]) {
		PriorityQueue<TimeState> que = new PriorityQueue<TimeState>();
		boolean[] visited = new boolean[nStation + 1];
		
		que.add(new TimeState(start, 0));
		
		while(!que.isEmpty()) {
			TimeState current = que.poll();
			if(visited[current.currentTown])
				continue;
			visited[current.currentTown] = true;
			
			if(current.currentTown == goal)
				return current.time;
			
			for(int i = 1; i <= nStation; i++) {
				if(time[current.currentTown][i] > 0) {
					que.add(new TimeState(i, current.time + time[current.currentTown][i]));
				}
			}
		}
		return -1;
	}
}

class CostState implements Comparable<CostState> {
	int currentTown;
	int cost;
	public CostState(int currentTown, int cost) {
		this.cost = cost;
		this.currentTown = currentTown;
	}
	
	@Override
	public int compareTo(CostState that) {
		if(this.cost < that.cost)
			return -1;
		else if(this.cost == that.cost)
			return 0;
		else
			return 1;
	}
}

class TimeState implements Comparable<TimeState> {
	int currentTown;
	int time;
	public TimeState(int currentTown, int time) {
		this.time = time;
		this.currentTown = currentTown;
	}
	
	@Override
	public int compareTo(TimeState that) {
		if(this.time < that.time)
			return -1;
		else if(this.time == that.time)
			return 0;
		else
			return 1;
	}
}